package main

import (
	"encoding/json"
	"fmt"
	"github.com/aliyun/alibaba-cloud-sdk-go/services/dcdn"
	"io/ioutil"
	"log"
)

func handleUploadDCdnCert(filePath string) {

	tempStr, err := ioutil.ReadFile(filePath)
	if err != nil {
		log.Panic(err)
	}
	configList := ConfigList{}
	err = json.Unmarshal(tempStr, &configList)
	if err != nil {
		log.Panic(err)
	}
	configList = formatConfig(configList)
	configList = checkFormatConfig(configList)

	for _, config := range configList.List {
		uploadCert(config)
	}
}

func uploadCert(config Config) {
	//"cn-hangzhou"
	client, err := dcdn.NewClientWithAccessKey(string(config.RegionId), string(config.AccessKeyId), string(config.AccessSecret))
	if err != nil {
		log.Panic(err.Error())
	}
	request := dcdn.CreateSetDcdnDomainCertificateRequest()
	request.Scheme = "https"

	request.DomainName = string(config.DomainName)   //".xx.com"
	request.SSLProtocol = string(config.SSLProtocol) //"on"
	request.CertName = string(config.CertName)       //"*.xx.com"
	request.CertType = string(config.CertType)       //"upload"
	request.SSLPub = string(config.sSLPub)           //"xx.pub"
	request.SSLPri = string(config.sSLPri)           //"xx.key"
	request.ForceSet = string(config.ForceSet)       // "1"

	response, err := client.SetDcdnDomainCertificate(request)
	if err != nil {
		fmt.Println("Upload Fail:", err.Error())
	} else {
		fmt.Println("Upload Success")
	}
	fmt.Printf("response is %#v\n", response)
}
